From 3fc405003d5d045a9f4ddf234dc5c507e479c2e2 Mon Sep 17 00:00:00 2001 From: Christian Korber Date: Wed, 14 Aug 2024 14:57:08 +0200 Subject: [PATCH] luci-app-snmpd: add MIB download area This commit adds a new tab to download all mib files that are stored in /usr/share/snmp/mibs folder. Signed-off-by: Christian Korber --- .../resources/view/snmpd/download.js | 83 +++++++++++++++++++ .../usr/share/luci/menu.d/luci-app-snmpd.json | 28 ++++++- .../usr/share/rpcd/acl.d/luci-app-snmpd.json | 6 +- 3 files changed, 115 insertions(+), 2 deletions(-) create mode 100644 applications/luci-app-snmpd/htdocs/luci-static/resources/view/snmpd/download.js diff --git a/applications/luci-app-snmpd/htdocs/luci-static/resources/view/snmpd/download.js b/applications/luci-app-snmpd/htdocs/luci-static/resources/view/snmpd/download.js new file mode 100644 index 0000000000..0fc147adc4 --- /dev/null +++ b/applications/luci-app-snmpd/htdocs/luci-static/resources/view/snmpd/download.js @@ -0,0 +1,83 @@ +'use strict'; +'require form'; +'require fs'; +'require rpc'; +'require ui'; +'require view'; + +var mapdata = { actions: {} }; + +var mibDownload = rpc.declare({ + object: 'file', + method: 'read', + params: [ 'path' ], + expect: { data : '' }, +}); + +return L.view.extend({ + handleMIB: function(name) { + const base = '/usr/share/snmp/mibs/'; + const fileName = name; + const a = document.createElement('a'); + + document.body.appendChild(a); + a.display = 'none'; + + return mibDownload(base + fileName, false).then(function(res) { + const data = res; + const file = new Blob( [data] , { type: 'text/plain'}); + const fileUrl = window.URL.createObjectURL(file); + + a.href = fileUrl; + a.download = fileName; + a.click(); + document.body.removeChild(a); + }); + }, + + load: function() { + return Promise.all([ + L.resolveDefault(fs.list('/usr/share/snmp/mibs/'), []) + .then(function(entries) { + const files = []; + entries.forEach((elem) => { + if (elem.type == 'file' && + elem.name.match(/MIB\.(mib|my|txt)$/i)) { + files.push(elem.name); + } + }); + return files; + }) + ]); + }, + + render: function(data) { + let m, s, o, ss; + const files = data[0]; + + m = new form.JSONMap(mapdata, _('SNMP - MIB Download'), + 'Here you can download SNMP-MIB files'); + + s = m.section(form.NamedSection, 'actions', _('Actions')); + + o = s.option(form.SectionValue, 'actions', + form.NamedSection, 'actions', 'actions', + _('Download Area')); + + ss = o.subsection; + + files.forEach((elem) => { + o = ss.option(form.Button, 'dl_mib', _(elem), + _('')); + o.inputstyle = 'action important'; + o.inputtitle = _('Download'); + o.onclick = ui.createHandlerFn(this, this.handleMIB, elem); + }); + + return m.render(); + }, + + handleSaveApply: null, + handleSave: null, + handleReset: null + }); diff --git a/applications/luci-app-snmpd/root/usr/share/luci/menu.d/luci-app-snmpd.json b/applications/luci-app-snmpd/root/usr/share/luci/menu.d/luci-app-snmpd.json index 85850a218a..b043a57edc 100644 --- a/applications/luci-app-snmpd/root/usr/share/luci/menu.d/luci-app-snmpd.json +++ b/applications/luci-app-snmpd/root/usr/share/luci/menu.d/luci-app-snmpd.json @@ -1,13 +1,39 @@ { "admin/services/snmpd": { "title": "SNMPD", + "order": 5, + "action": { + "type": "alias", + "path": "admin/services/snmpd/snmpd" + }, + "depends": { + "acl": [ "luci-app-snmpd" ], + "uci": { "snmpd": true } + } + }, + + "admin/services/snmpd/snmpd": { + "title": "SNMP", + "order": 10, "action": { "type": "view", "path": "snmpd/snmpd" }, + "depends": { + "acl": [ "luci-app-snmpd" ] + } + }, + + "admin/services/snmpd/download": { + "title": "Download", + "order": 20, + "action": { + "type": "view", + "path": "snmpd/download" + }, "depends": { "acl": [ "luci-app-snmpd" ], - "uci": { "snmpd": true } + "fs": [ { "/usr/share/snmp/mibs/": "directory" } ] } } } diff --git a/applications/luci-app-snmpd/root/usr/share/rpcd/acl.d/luci-app-snmpd.json b/applications/luci-app-snmpd/root/usr/share/rpcd/acl.d/luci-app-snmpd.json index 566e521391..2fdcd59a5f 100644 --- a/applications/luci-app-snmpd/root/usr/share/rpcd/acl.d/luci-app-snmpd.json +++ b/applications/luci-app-snmpd/root/usr/share/rpcd/acl.d/luci-app-snmpd.json @@ -2,7 +2,11 @@ "luci-app-snmpd": { "description": "Grant UCI access for luci-app-snmpd", "read": { - "uci": [ "snmpd" ] + "uci": [ "snmpd" ], + "file": { + "/usr/share/snmp/mibs/*": [ "read", "stat" ], + "/usr/bin/ldd /usr/lib/libnetsnmp.so.40": [ "exec" ] + } }, "write": { "uci": [ "snmpd" ] -- 2.30.2